<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

$Id$

Notes:
--->
<cfcomponent
	displayname="LoginGateway"
	output="false"
	extends="LoginGateway"
	hint="LoginGateway for MachBlog (PostgreSQL)">

	<cffunction name="init" access="public" output="false" returntype="LoginGateway"
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>

	<cffunction name="validateLogin" access="public" output="false" returntype="boolean"
			hint="Validates the login based on the email and password passed in">
		<cfargument name="email" type="string" required="true" />
		<cfargument name="password" type="string" required="true" />

		<cfset var checkLogin = "" />
		<cfset var updateLastAccess = "" />
		<cfset var success = false />
		<cfset var user = getUserService().getUserBean() />
		<cfset var audit = createObject("component", "machblog.org.machblog.audit.Audit") />
		<cfset var dtLastAccess = 0 />
		<cfset var dtModified = 0 />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		
		<cftry>
			<cfquery name="checkLogin" datasource="#getDatasource().getDsn()#"
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT 	user_id
				FROM 	machblog_user
				WHERE 	email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="100" />
				AND		password = <cfqueryparam value="#arguments.password#" cfsqltype="cf_sql_varchar" maxlength="50" />
				AND 	is_active = 1
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>

		<!--- if the login succeeded, update the last access fields, initialize the objects, and
				start the session --->
		<cfif IsQuery(checkLogin) AND checkLogin.RecordCount EQ 1>
			<!--- update last access in db --->
			<cftry>
				<cfquery name="updateLastAccess" datasource="#getDatasource().getDsn()#"
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
					UPDATE 	machblog_user
					SET 	dt_last_access = <cfqueryparam value="#Now()#" cfsqltype="cf_sql_bigint" />,
							ip_last_access = <cfqueryparam value="#CGI.REMOTE_ADDR#"
															cfsqltype="cf_sql_varchar"
															maxlength="15" />
					WHERE 	user_id = <cfqueryparam value="#checkLogin.user_id#" cfsqltype="cf_sql_char"
													maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<!--- just proceed--not a huge deal if this bombs for some reason --->
				</cfcatch>
			</cftry>

			<cfset success = true />

			<!--- populate the user object --->
			<cfset user.setUserId(checkLogin.user_id) />
			<cfset getUserService().getUser(user) />

			<!--- start up the session --->
			<cfset getUserFacade().setUser(user) />

			<!--- login failed or query bombed --->
			<cfelse>
				<cfset success = false />
		</cfif>

		<cfreturn success />
	</cffunction>

	<cffunction name="checkEmailExists" access="public" output="false" returntype="boolean"
			hint="Checks to see if the email address passed in exists as a user in the system">
		<cfargument name="email" type="string" required="true" />

		<cfset var emailExists = false />
		<cfset var getEmail = "" />

		<cftry>
			<cfquery name="getEmail" datasource="#getDatasource().getDsn()#"
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT email
				FROM machblog_user
				WHERE email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="100" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>

		<cfif IsQuery(getEmail) AND getEmail.RecordCount GT 0>
			<cfset emailExists = true />
		</cfif>

		<cfreturn emailExists />
	</cffunction>

	<cffunction name="getUserIdByEmailAddress" access="public" output="false" returntype="string"
			hint="Returns the user ID for the email address passed in">
		<cfargument name="email" type="string" required="true" />

		<cfset var getUserId = "" />
		<cfset var userId = "" />

		<cftry>
			<cfquery name="getUserId" datasource="#getDatasource().getDsn()#"
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT user_id
				FROM machblog_user
				WHERE email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="100" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>

		<cfif IsQuery(getUserId) AND getUserId.RecordCount GT 0>
			<cfset userId = getUserId.user_id />
		</cfif>

		<cfreturn userId />
	</cffunction>

	<cffunction name="updatePassword" access="public" output="false" returntype="void"
			hint="Updates a user's password using the user ID and (pre-hashed) password passed in">
		<cfargument name="userId" type="uuid" required="true" />
		<cfargument name="password" type="string" required="true" />

		<cfset var updatePassword = "" />

		<cftry>
			<cfquery name="updatePassword" datasource="#getDatasource().getDsn()#"
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				UPDATE 	machblog_user
				SET 	password = <cfqueryparam value="#arguments.password#" cfsqltype="cf_sql_varchar" maxlength="50" />
				WHERE 	user_id = <cfqueryparam value="#arguments.userId#" cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>

	<cffunction name="logout" access="public" output="false" returntype="void"
			hint="Logs the user out by removing the user via the UserFacade">
		<cfset getUserFacade().removeUser() />
	</cffunction>
</cfcomponent>